import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
import router from './router/router.js' //路由模块
import axios from './common/axios-td.js' //请求模块
import webconfig from './config.js' //网站配置
import base from './common/base.js' //基础方法
import common from './common/common.js' //基础方法
import store from './store/store.js' //全局状态管理
import Components from './router/components.js' //注册全局组件
import layer from './common/layer.js' //弹窗
import image from './common/image.js' //图片处理
import wechat from './common/wechat-td.js' //微信相关
import wxsdk from 'weixin-js-sdk'; //微信jssdk
import myLocalStorage from './common/localStorageHelper.js' //缓存管理

Vue.config.productionTip = false
Vue.use(Vuex)

/*Swiper */
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper)

// vant组件注册
import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

//


/*样式注册 */
import '@/assets/css/common.css'
import '@/assets/css/style.css'

// 全局js注册
Vue.config.productionTip = false
Vue.prototype.$ajax = axios
Vue.prototype.webconfig = webconfig
Vue.prototype.base = base
Vue.prototype.wechat = wechat;
Vue.prototype.common = common;
Vue.prototype.wxsdk = wxsdk;
Vue.prototype.image = image;
Vue.prototype.$storage = myLocalStorage

// 注入全局过滤器
import * as filter from '@/common/filter'
Object.keys(filter).forEach(key => {
  Vue.filter(key, filter[key])
})

// 注入全局指令
import * as directives from '@/common/directives'
Object.keys(directives).forEach(key => {
  Vue.directive(key, directives[key])
})

// eslint-disable-next-line no-unused-vars
function GetUrlParam(_name) {
  var reg = new RegExp('(^|&)' + _name + '=([^&]*)(&|$)');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null
}
//页面跳转前逻辑处理
router.beforeEach((to, from, next) => {
  var prevname='';
  if(from.name) {
    prevname=from.name
  }
  window.localStorage.setItem('prevurl',prevname)
  //页面标题
  var webtit = to.meta.title || '';
  webtit = webtit.length > 0 ? webtit + '-' : webtit;
  window.document.title = webtit + webconfig.title
  //背景处理
  var bg = to.meta.bg || '';
  if (bg.length > 0) {
    document.getElementById('body').style.background = bg;
  } else {
    document.getElementById('body').style.background = '';
  }

  //登录逻辑
  var login = to.meta.login;
  if (login) {
    var token = window.localStorage.getItem('usertoken') || '';
    if (token.length <= 0) {
      WeChatLoginHandler(to, from, next)
      return;
    }
    //这里可以进行登录时效性判断，但是得自己去做

    //去除？后面的参数
    var url = window.location.href
    if (url.indexOf('?') >= 0) {
      url = webconfig.login + '/#' + to.path
      window.location.href = url
      return;
    }
    next();
    return;
  }
  //去除？后面的参数
  url = window.location.href
  if (url.indexOf('?') >= 0) {
    url = webconfig.login + '/#' + to.path
    window.location.href = url
    return;
  }
  next();
})



function WeChatLoginHandler(to, from, next) {
  //判断是否需要进行微信登录
  if (!webconfig.wechatlogin) {
    router.push('/login');
    return;
  }
  //调用微信登录接口
  wechat.WeChatLogin(to.path, () => {
    next();
    return;
  }, (_msg) => {
    layer.msg(_msg)
  })
  //模拟
  // var data=webconfig.user;// {unionId:'test0123456',openId:'123456789',nickName:'仙杜瑞拉',gender:1,avatar:'http://data.liujto.com/avatar1.jpg'}
  // wechat.Login(data,()=>{
  //   next();
  //   return;
  // },()=>{
  //   //失败
  //   return;
  // })
}

function GetWebSocketData(_action,_data) {
  return {
    action:_action,
    uid:base.GetFingerprint(),
    appid:webconfig.appid,
    data:JSON.stringify(_data)
  }
}


new Vue({
  router,
  store,
  Components,
  render: h => h(App),
  created() {
    //创建成功时 才创建layer对象
    Vue.prototype.layer = layer;
    
  },
}).$mount('#app')